$(document).ready(function() {		
	var limit = 8;
	var count = 1;
	var category = "All";
	
	var sort = "";
	
	//FIND FIRST AND LAST GIG
	$('.post-listing li').first().addClass('first');
	$('.post-listing li').last().addClass('last');
	
	//SORT
	$('.sortby > span').click(function(){
		$('.sortby > span').removeClass('active');
		$(this).addClass('active');
		
		count = 1;
		loadData();
		changeHeight();
	})
		
	var sortglobal = true;
	
	$('ul.post-listing li').addClass("show");
	
	//FILTER
	$('.filter-category li span').click(function(){
		var filterCategory = $(this).text();
		var gigCategoryName = $('.post-listing .post-action .category');
		var keyword = $('.keywordarea span span');			
		if ($(this).parent().find("input[type=checkbox]").is(":checked")) {
			category = "";
			$(".keywordarea span span").each(function () {
				if ($(this).text() == filterCategory) {
					$(this).parent().remove();
					if ($('.keywordarea').children().length == 0 ) {
						category = "All";
						$('.keywordarea').append('<span><span>All</span></span>');
						$('.keywordarea > span').addClass('all');
						$('ul.post-listing li').css('display','block').hide().fadeIn(100);
						$('ul.post-listing li').addClass("show");
					}
					
				} else {
					if (category != "") {
						category += ",";
					}
					category += $.trim($(this).text());
				}
			});
			
			loadData();
			changeHeight();
		} else {
			if (filterCategory == "All") {
				category = "All";
				$('.keywordarea span').remove();
				$('.keywordarea').append('<span><span>'+filterCategory+'</span></span>');
				$('.keywordarea > span').addClass('all');
				$(".overview .loading").css("display", "block");
				
				$(".filter-category li input[type=checkbox]").attr("checked", false);
				
				count = 1;
				loadData();
				
			} else {
				$(".filter-category li input[type=checkbox]").first().attr("checked", false);
				var exists = false;
				
				$(".keywordarea span span").each(function () {
					
					if ($(this).text() == filterCategory) {
						exists = true;
					}
				});
				if (!exists) {
					$('.keywordarea').append('<span><span>'+filterCategory+'</span></span>');
					$('.keywordarea span:last').hide().fadeIn(100);
					//Add content form category to 
					if ($.trim($(".keywordarea span:first span").html()) == "All") {
						$('.all').remove().fadeOut('slow');
					}
					$(".keywordarea > span").unbind("click");
					$(".keywordarea > span").bind("click", keywordRemove);
					
					category = "";
					$(".keywordarea span span").each(function () {
						if (category != "") {
								category += ",";
						}
						category += $.trim($(this).text());
					});
					
					$(".overview .loading").css("display", "block");
					
					count = 1;
					
					loadData();
				}
			}
			changeHeight();
		}
		
		
	});
	
	function keywordRemove() {
		var txt = $(this).find("span").text();
		
		$(this).remove();
		
		$('.filter-category li span').each(function () {
			if ($(this).text() == txt) {
				$(this).parent().find("input[type=checkbox]").attr("checked", false);
			}
		});
		
		if ($('.keywordarea').children().length == 0 ) {
			$('.keywordarea').append('<span><span>All</span></span>');
			$('.keywordarea > span').addClass('all');
			$('ul.post-listing li').css('display','block').hide().fadeIn(100);
			$('ul.post-listing li').addClass("show");
		}
		
		category = "";
		$(".keywordarea span span").each(function () {
			if (category != "") {
					category += ",";
			}
			category += $.trim($(this).text());
		});
		
		count = 1;
		loadData();
	}
	
	$("#gotop").css("display", "none");
	
	$("#gotop").click(function () {
		//$('#scrollbar1').tinyscrollbar_update(0);
		$(".overview").animate({top: 0}, 500);
		$(".thumb").animate({top: 0}, 500);
		setTimeout ( function() {
			$('#scrollbar1').tinyscrollbar_update(0);
		}, 500 );
		$(this).css("display", "block");
	});
	bindMouse();
	function bindMouse() {
		$(".overview").bind('DOMMouseScroll', eventScroll);
		$(".overview").bind('mousewheel', eventScroll);
		$(".scrollbar").bind('mousemove', eventScroll);
		$(".thumb").bind('mousedown', eventScroll);
		$(".track").bind('mouseup', eventScroll);
		$(".overview").bind('touchmove', eventScroll);
		$(".scrollbar").bind('touchmove', eventScroll);
	}
	
	function unbindMouse() {
		$(".overview").unbind('DOMMouseScroll', eventScroll);
		$(".overview").unbind('mousewheel', eventScroll);
		$(".scrollbar").unbind('mousemove', eventScroll);
		$(".thumb").unbind('mousedown', eventScroll);
		$(".track").unbind('mouseup', eventScroll);
		$(".overview").unbind('touchmove', eventScroll);
		$(".scrollbar").unbind('touchmove', eventScroll);
	}
	
	var process = false;
	function eventScroll() {
		var scrollHeight = parseInt($(".scrollbar").css("height"));
		var height = parseInt($(".thumb").css("height")) + parseInt($(".thumb").css("top"));
		if (scrollHeight >= height && height > scrollHeight - 20) {
			if (limit*count <= $("ul.post-listing li.clearafter.show").length && process == false) {
				process = true;
				count ++;
				loadData();
			} else {
				$(".overview .loading").css("display", "none");
			}
		}
		if ($("ul.post-listing li.clearafter.show").length > limit && parseInt($(".thumb").css("top")) > 50) {
			$("#gotop").css("display", "block");
		}
	}
	
	function loadData() {
		sort = $(".sortby span.active").html();
		unbindMouse();
		$(".overview .loading").css("display", "block");
		$.get('gig/ajax_gig.php', {page: count, category: category, sort: sort}, function (data) {
			setTimeout ( function() {
				if(count == 1) {
					$('ul.post-listing').html($("ul.post-listing li.loading"));
				}
				$("ul.post-listing").append(data);
				$('ul.post-listing').append($("ul.post-listing li.loading"));
				
				displayLimit();
				
				$('#scrollbar1').tinyscrollbar_update('relative');
				
				bindMouse();
				process = false;
				}, 500 );
			
		});
	}
	
	
	displayLimit();
	
	function displayLimit() {
		//Display limit content
		$(".overview .loading").addClass("show").addClass("display");
		$(".overview .loading").css("display", "block");
		$("ul.post-listing li").removeClass("display");
		//$("ul.post-listing li").css("display", "none").hide();
		$("ul.post-listing li.clearafter.show:lt("+(limit*count)+")").addClass("display");
		$("ul.post-listing li.clearafter.display").css("display", "block");//.fadeIn("slow");
		if (count > 1) {
			$("ul.post-listing li.clearafter.display:gt("+(limit*(count-1)-4)+")").hide().fadeIn(500);
		} else {
			$("ul.post-listing li.clearafter.display:gt("+(limit*(count-1))+")").hide().fadeIn(500);
		}
		
		if ($("ul.post-listing li.clearafter.show").length <= limit) {
			$(".overview .loading").removeClass("show").removeClass("display");
			$(".overview .loading").css("display", "none");
			$("#gotop").css("display", "none");
		}
		
		if (limit*count < $("ul.post-listing li.clearafter.show").length) {
			$(".overview .loading").addClass("show").addClass("display");
			$(".overview .loading").css("display", "block");
		}
		
	}
	changeHeight();
	function changeHeight() {
		if ($(".post-listing li.clearafter.display").length < limit) {
			$("#scrollbar1 .viewport").css("height", $(".post-listing").height());
			$('#scrollbar1').tinyscrollbar({ lockscroll: false});
			$(".scrollbar-wrap").css("display", "none");
			$("#gotop").css("display", "block");
		} else {
			$("#scrollbar1 .viewport").css("height", 730);
			$('#scrollbar1').tinyscrollbar();
			$(".scrollbar-wrap").css("display", "block");
			
		}
		
		$('#scrollbar1').tinyscrollbar_update();
	}
	
});